<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Forms\TagForm;
use App\Modules\System\Models\Tag;
use App\Modules\Shop\Shop;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Modules\User\Models\Admin;

class TagController extends Controller
{

    public function index(Request $request)
    {
        $this->checkAuth('staff_tag', 'tag');
        $shopId = $request->input('shop_id');
        $name = $request->input('name', '');
        $data = Tag::query()
            ->when(!empty($name), function ($query) use ($name) {
                return $query->where('name', 'like', '%' . $name . "%");
            })
            ->where('shop_id', $shopId)
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->paginate(20);
        $shopData = Shop::query()->find($shopId);
        
        $sidebar = 'tag';
        return view('tag.index', compact('data', 'shopId', 'sidebar', 'name'));
    }

    public function create(Request $request)
    {
        $this->checkAuth('staff_tag-add', 'tag-add');

        $shopId = $request->input('shop_id');

        $shopData = Shop::query()->find($shopId);

        $form = $this->form(TagForm::class, [
            'url' => route('admin.tag.store', ['shop_id' => $shopId]),
            'method' => 'POST',
        ]);
       
        return view('tag.form', compact('form', 'shopId'));
    }

    public function store(Request $request)
    {
        $this->checkAuth('staff_tag-add', 'tag-add');
        $shopId = $request->input('shop_id');
        $form = $this->form(TagForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        $data['shop_id'] = $shopId;
        $data['admin_id'] = Auth::user()->id;

        Tag::query()->create($data);
        return redirect()->route('admin.tag.index', ['shop_id' => $data['shop_id']]);
    }

    public function edit(Tag $tag, Request $request)
    {
        $this->checkAuth('staff_tag-edit', 'tag-edit');
        $shopId = $request->input('shop_id');

        $shopData = Shop::query()->find($shopId);
        
        $form = $this->form(TagForm::class, [
            'url' => route('admin.tag.update', ['tag' => $tag, 'shop_id' => $shopId]),
            'method' => 'PUT',
            'model' => $tag
        ]);
        return view('tag.form', compact('form'));
    }

    public function update(Tag $tag, Request $request)
    {
        $this->checkAuth('staff_tag-edit', 'tag-edit');
        $shopId = $request->input('shop_id');
        $form = $this->form(TagForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        $data['admin_id'] = Auth::user()->id;
        $tag->update($data);
        return redirect()->route('admin.tag.index', ['shop_id' => $shopId]);
    }

    public function destroy(Tag $tag)
    {
        $this->checkAuth('staff_tag-del', 'tag-del');
        $tag->delete();
        return redirect()->route('admin.tag.index', ['shop_id' => $tag->shop_id]);
    }

    public function getTag(Request $request)
    {
        $shop_id = $request->input('shop_id');
        $re = Tag::query()
            ->where('shop_id', $shop_id)
            ->get()
            ->pluck('name', 'id');
        return $re;
    }

    public function changeSort(Request $request)
    {
        $id = $request->input('id');
        $sort = $request->input('sort');
        Tag::query()
            ->where('id', $id)
            ->update(['sort' => $sort]);
        return redirect()->back();
    }
}
